package com.bleem.ds.xinshou.a02;

/**
 * @author SuYanchun
 * @since 2024-08-07 下午1:53
 */
public class RandomTest {
    public static void main(String[] args) {
        //已知f1等概率返回 3～9 要求使用f1编写g函数，等概率返回34～59
        int testNum = 1000000;
        int[] count = new int[100];
        int probability = 0;
        for (int i = 0; i < testNum; i++) {
            int random = g();
            count[random]++;
            if (random == 49 ) {
                probability++;
            }
        }


        System.out.println("出现的概率：" + Double.parseDouble(String.valueOf(probability)) / Double.parseDouble(String.valueOf(testNum)));
        for (int i = 0; i < count.length; i++) {
            System.out.println(i+"出现了"+count[i]+"次");
        }
    }

    public static int f1(){
        return (int) (Math.random() * 7) + 3;
    }

    public static int f2(){
        int result;
        do {
            result = f1();
        } while (result == 6);
        return result < 6 ? 0 : 1;
    }

    public static int f3(){
        return (f2() << 5) + (f2() << 4) + (f2() << 3) + (f2() << 2) + (f2() << 1) + f2();
    }

    public static int g() {
        int result;
        do {
            result = f3();
        } while (result > 25);

        return result + 34;
    }

}
